<?php include "header.php"; ?>
	<script type = "text/javascript">
		function change() {
			document.getElementById("updateInfo").submit();
		}
	</script>

	<?php 
		if (!isset($_SESSION["email"])) {  //if the user is not logged in
			echo "You must be logged in to view this page.";
			require("footer.php");
			die;
		}
		else { //If the user is logged in
			$email = $_SESSION['email'];
			$currentCar = "";
			
			if ($_SERVER["REQUEST_METHOD"] == "POST") {  //if form data has been submitted
				$phone =  ($_POST["phone"]);
				$street_address =  ($_POST["street_address"]);
				$city_address =  ($_POST["city_address"]); 
				$state_address =  ($_POST["state_address"]);
				$zip_address =  ($_POST["zip_address"]);
				$make =  ($_POST["make"]);
				$model =  ($_POST["model"]);
				$year =  ($_POST["year"]);
				
				//Updates the user table with the car ID if the car exists. 
				$query = "SELECT car_id FROM car WHERE make = '" . $make . "' AND model = '" . $model . "' AND year = '" . $year . "'";
				if ($result = mysqli_query($con, $query)) {
					if (mysqli_num_rows($result) > 0) {
						while ($row = mysqli_fetch_array($result)) {
							$car_id = $row["car_id"];
						}
						$query = "update user set owned_car = '" . $car_id . "' WHERE email = '" . $email . "'";
						if (!mysqli_query($con, $query))
							echo mysqli_error($con);
						$currentCar = "Your car has been successfully updated to a " . $year . " " . $make . " " . $model;
					}
				}
				
				//If the user already has an address, update it instead. 
				if (entry_exists('address', 'email', $email)) {
					$query = "update address set street='$street_address', city='$city_address', state='$state_address', zip='$zip_address' where email='$email' ";
					mysqli_query($con, $query);
				}
				//Creates a new database entry for the address of this user. 
				else {
					$query = "INSERT into address (email, street, city, state, zip) values('$email', '$street_address', '$city_address', '$state_address', '$zip_address')";
					if(!mysqli_query($con, $query))
						echo mysqli_error($con);
				}
				
				//Updates the user table with the chosen branch
				if (isset($_POST["branch"]) && ($_POST["branch"] != "empty")) {
					$query = "UPDATE user set branch_preference=' " . $_POST["branch"] . " ' WHERE email='$email' ";
					if(mysqli_query($con, $query))
						$currentBranch = $_POST["branch"];
					else
						echo mysqli_error($con);
				}
				else
					$currentBranch = "";
			}
			
			else {  //If there has not been any form data submitted.
				$phone = $street_address = $city_address = $state_address = $zip_address = ' ';
				$make = "Select Make";
				$model = "Select Model";
				$year = "Select Year";
				
				
				//Query to find the address information for the user. 
				$query = "SELECT phone, street, city, state, zip FROM user natural join address WHERE email = '" . $email . "'";
				$result = mysqli_query($con, $query);
				
				while ($row = mysqli_fetch_array($result))
				{
					$phone = $row["phone"] ;
					$street_address = $row["street"];
					$city_address = $row["city"];
					$state_address = $row["state"];
					$zip_address = $row["zip"];
				}
				
				//Query to find the car information for the user. 
				$query = "SELECT make, model, year FROM car join user ON car.car_id = user.owned_car WHERE email= '" . $email . "'";
				if ($result = mysqli_query($con, $query)) {
					while ($row = mysqli_fetch_array($result))
					{
						$currentCar = "Your current vehicle is a " . $row['year'] . " " . $row['make'] . " " . $row['model'] . ". You may update your information below.";
					}
				}
				
				//Query to find the current preferred branch for the user.
				$query = "SELECT branch_preference FROM user WHERE email='" . $email . "'";
				if ($result = mysqli_query($con, $query)) {
					while ($row = mysqli_fetch_array($result))
					{
						$currentBranch = trim($row["branch_preference"], " ");
					}
				}
				
			}
		}
		
		//This function will return true if there is any data in the table $table_name which has $key == $index
		function entry_exists ($table_name, $key, $index)
		{
			global $con;
			$query = "SELECT * FROM " . $table_name . " WHERE " . $key  . " = '" . $index . "'";
			if ($result = mysqli_query($con, $query)) {
				if (mysqli_num_rows($result) > 0)
					return true;
			}
			return false; 
		}
	?>
	
	<form id="updateInfo" method = "POST" action="">
		<h2> Contact Information </h2>
		<br>Phone Number: <input type="text" name="phone" value="<?php echo $phone?>"><br><br>
		
		<h3> Address </h3>
		Street: <input type="text" name="street_address" value="<?php echo $street_address?>"><br>
		City: <input type="text" name="city_address" value="<?php echo $city_address?>"><br>
		State: <input type="text" name="state_address" value="<?php echo $state_address?>"><br>
		ZIP Code: <input type="text" name="zip_address" value="<?php echo $zip_address?>"><br>
		
		<br><br>
		
		<h2> Car Information </h2>
			<?php 
				echo $currentCar . "<br><br>";

				if(!isset($_POST["make"]) or ($_POST["make"] == "default")) {
					$make = "----Select Make----";
				}
				else
					$make = $_POST["make"];				
				echo 'Make: <select name="make" onchange="change()">
					<option value="' . $make . '">' . $make . '</option>';
				$query = "SELECT distinct make FROM car WHERE car_id!=-1";
				if ($result = mysqli_query($con, $query)) {
					if (mysqli_num_rows($result) > 0) {
						while ($row = mysqli_fetch_array($result)) {
							echo '<option value="' . $row["make"] . '">' . $row["make"] . '</option>';
						}
					}
				}
				echo '</select>';

				if(!isset($_POST["make"]) or ($_POST["make"] == "default")) {
					$model = "----Select Make----";
				}
				else if (!isset($_POST["model"]) or ($_POST["model"] == "default")) {
					$model = "----Select Model----";
				}
				else
					$model = $_POST["model"];

				echo 'Model: <select name="model" onchange="change()">';
				echo '<option value="' . $model . '">' . $model . '</option>';
				$query = "SELECT distinct model FROM car WHERE make = '" . $make . "'";
				if ($result = mysqli_query($con, $query)) {
					if (mysqli_num_rows($result) > 0) {
						while ($row = mysqli_fetch_array($result)) {
							echo '<option value="' . $row["model"] . '">' . $row["model"] . '</option>';
						}
					}
				}
				echo '</select>';

				if(!isset($_POST["make"]) or ($_POST["make"] == "default")) {
					$year = '----Select Make----';
				}
				else if (!isset($_POST["model"]) or ($_POST["model"] == "default")) {
					$year = '----Select Model----';
				}
				else if (!isset($_POST["year"]) or ($_POST["year"] == "default")) {
					$year = '----Select Year----';
				}
				else {
					$year = $_POST["year"];
				}

				echo 'Year: <select name="year">';
				echo '<option value="' . $year . '">' . $year . '</option>';
				$query = "SELECT distinct year FROM car WHERE make = '" . $make . "' AND model = '" . $model . "'";
				if ($result = mysqli_query($con, $query)) {
					if (mysqli_num_rows($result) > 0) {
						while ($row = mysqli_fetch_array($result)) {
							echo '<option value="' . $row["year"] . '">' . $row["year"] . '</option>';
						}
					}
				}
				echo '</select>'; 
			?>
		
		<br><br>
		
		<h2> Preferred Branch </h2>
		<?php 
			echo 'Select your preferred branch: <select name="branch"><option value = "empty">----Select Your Branch----</option>';
			$query = "SELECT name FROM branch";
			if ($result = mysqli_query($con, $query)) {
				if (mysqli_num_rows($result) > 0) {
					while ($row = mysqli_fetch_array($result)) {
						if ($row["name"] == $currentBranch)
							echo '<option selected value = "' . $row["name"] . '">' . $row["name"] . '</option>';
						else
							echo '<option value = "' . $row["name"] . '">' . $row["name"] . '</option>';
					}
				}
			}
			echo '</select>';
		?>
		</br></br>
		<input type="submit" name="update" value="Update Profile">
		
	</form>

<?php include "footer.php"; ?>